#include "xmlparse.hpp"

using namespace com_oocl_monitor::dao;

XmlParse::XmlParse()
{
	_pt.clear();
}
void XmlParse::parse(DbConfig &dbConfig, const string& fileName) {
	dbConfig.clear();
	read_xml(fileName, _pt);
	if (!_pt.empty()) {
		auto child = _pt.get_child("dbconfigs.dbconfig");
		dbConfig._constr = child.get<string>("connectstring");
		dbConfig._usr = child.get<string>("username");
		dbConfig._pwd = child.get<string>("password");
		dbConfig._maxCon = child.get("maxCon", 5);
		dbConfig._minCon = child.get("minCon", 0);
		dbConfig._incrCon = child.get("incrCon", 1);
	}
	_pt.clear();
}

void XmlParse::parse(EmailConfig &emailConfig, const string& fileName) {
	emailConfig.clear();
	read_xml(fileName, _pt);
	if (!_pt.empty()) {
		auto child = _pt.get_child("emailconfig");
		emailConfig._server = child.get<string>("server");
		emailConfig._port = child.get("port","");
		emailConfig._from = child.get<string>("from");
		emailConfig._usr = child.get("usr", "");
		emailConfig._pwd = child.get("pwd", "");
		child = _pt.get_child("emailconfig.tolist");
		for (auto x = child.begin();x != child.end();++x) {
			if(x->first=="to")
				emailConfig.toList.push_back(x->second.data());
		}
	}
	_pt.clear();
}
void XmlParse::parse(FtpProfileList &ftpProfileList, const string& fileName) {
	ftpProfileList.clear();
	read_xml(fileName, _pt);
	if (!_pt.empty()) {
		auto child = _pt.get_child("FtpProfileList");
		for (auto x = child.begin();x != child.end();++x){
			if (x->first == "ftp") {
				FtpInfo ftpInfo;
				ftpInfo._host = x->second.get<string>("host");
				ftpInfo._port = x->second.get("port", "");
				ftpInfo._usr = x->second.get("username", "");
				ftpInfo._pwd = x->second.get("password", "");
				ftpInfo._maxCon = x->second.get("maxcon", 5);
				auto y = x->second.get_child("profilelist");
				vector<Profile> profileList;
				for (auto z = y.begin();z != y.end();++z){
					if (z->first == "profile") {
						Profile profile;
						profile._warehouse = z->second.get("warehouse", "");
						profile._owner = z->second.get("owner", "");
						profile._profileName = z->second.get<string>("profilename");
						profile._orderType = z->second.get("ordertype", "");
						profile._dataDirection = z->second.get<string>("datadirection");
						profile._regexPattern = z->second.get("regexPattern", "");
						profile._uri = z->second.get<string>("uri");
						profile._project= z->second.get("project","WMS");
						profileList.push_back(profile);
					}
				}
				FtpProfile tmp;
				tmp._ftpInfo = ftpInfo;
				tmp._profiles = profileList;
				ftpProfileList.push_back(tmp);
			}
		}
	}
	_pt.clear();
}